#include "mainwidget.h"
#include "utils.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDir>
#include <QDebug>
#include "myQSS.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    //初始化UI
    initUI();

    // 连接信号槽
    ConnectSignalSlots();

    // 扫描歌曲
    scanSongs();
}

MainWidget::~MainWidget()
{
}

void MainWidget::initUI()
{
    this->setGeometry(0, 0, 800, 480);
    this->setObjectName("MainWidget");

    for(uint32_t i = 0; i < 5; i++){
        musicBtn[i] = new QPushButton(this);
        musicBtn[i]->setFocusPolicy(Qt::NoFocus);
        musicBtn[i]->setMinimumSize(30, 30);
        musicBtn[i]->setMaximumSize(30, 30);
    }

    musicBtn[0]->setObjectName("perBtn");
    musicBtn[1]->setObjectName("playBtn");
    musicBtn[2]->setObjectName("nextBtn");
    musicBtn[3]->setObjectName("modelBtn");
    musicBtn[4]->setObjectName("volBtn");

    //创建弹簧空白区 一个new为一个对象不能重用
    QSpacerItem *spacer1 = new QSpacerItem(80, 60, QSizePolicy::Maximum, QSizePolicy::Maximum);
    QSpacerItem *spacer2 = new QSpacerItem(80, 60, QSizePolicy::Maximum, QSizePolicy::Maximum);
    QSpacerItem *spacer3 = new QSpacerItem(80, 60, QSizePolicy::Maximum, QSizePolicy::Maximum);
    QSpacerItem *spacer4 = new QSpacerItem(80, 60, QSizePolicy::Maximum, QSizePolicy::Maximum);

    QWidget *btnW = new QWidget(this);
    QHBoxLayout* btnLayout = new QHBoxLayout(btnW);
    btnLayout->addStretch();
    btnLayout->addWidget(musicBtn[0]);
    btnLayout->addSpacerItem(spacer1);
    btnLayout->addWidget(musicBtn[1]);
    btnLayout->addSpacerItem(spacer2);
    btnLayout->addWidget(musicBtn[2]);
    btnLayout->addSpacerItem(spacer3);
    btnLayout->addWidget(musicBtn[3]);
    btnLayout->addSpacerItem(spacer4);
    btnLayout->addWidget(musicBtn[4]);
    btnLayout->addStretch();
    btnLayout->setContentsMargins(0, 30, 0, 20);
    btnLayout->setSpacing(0);

    durationslider = new DurationSlider(this);

    QLabel *picLab = new QLabel(this);
    picLab->setAlignment(Qt::AlignCenter);
    picLab->setPixmap(QPixmap(":/images/cd.png"));

    QWidget *rightW = new QWidget(this);
    QVBoxLayout *rightLayout = new QVBoxLayout(rightW);
    rightLayout->addStretch();
    rightLayout->addWidget(picLab);
    rightLayout->addStretch();
    rightLayout->addWidget(durationslider);
    rightLayout->addWidget(btnW);
    rightLayout->setMargin(0);
    rightLayout->setSpacing(0);

    musicList = new QListWidget(this);
    musicList->setObjectName("musicList");
    musicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //禁用水平滑动条
    musicList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);   //禁用垂直滚动条
    musicList->setFocusPolicy(Qt::NoFocus);
    musicList->setFixedSize(310, 420);
    musicList->setFrameShape(QListWidget::NoFrame); // 边框透明

//    listMask = new QWidget(musicList);
//    listMask->setMinimumSize(310, 50);
//    listMask->setMinimumHeight(50);
//    listMask->setObjectName("listMask");
//    listMask->setGeometry(0, musicList->height() - 50, 310, 50);

    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(musicList);
    mainLayout->addWidget(rightW);
    mainLayout->setStretch(0, 0);
    mainLayout->setStretch(1, 1);
//    mainLayout->setSpacing(10);


    // 以对象名启用槽
    QMetaObject::connectSlotsByName(this);
    /* 设置qss */
    Utils::setStyle(":/qss/mainWin.qss");
}

void MainWidget::ConnectSignalSlots()
{
    connect(&playerQt, &FFplayerQt::sendEvent, this, &MainWidget::onPlayerEvent);
}

void MainWidget::scanSongs()
{
    QDir dir(QCoreApplication::applicationDirPath()
             + "/myMusic");
    QDir dirbsolutePath(dir.absolutePath());
    /* 如果目录存在 */
    if (dirbsolutePath.exists()) {
        /* 定义过滤器 */
        QStringList filter;
        /* 包含所有.mp3后缀的文件 */
        filter << "*.mp3";
        /* 获取该目录下的所有文件 */
        QFileInfoList files = dirbsolutePath.entryInfoList(filter, QDir::Files);
        /* 遍历 */
        for (int i = 0; i < files.count(); i++) {
            /* 使用utf-8编码 */
            MusicInfo info;
            info.fileName = QString::fromUtf8(files.at(i)
                                                 .fileName()
                                                 .replace(".mp3", "")
                                                 .toUtf8()
                                                 .data());

            info.filePath = QString::fromUtf8(files.at(i)
                                              .filePath()
                                              .toUtf8()
                                              .data());

            /* 添加进容器 */
            musicInfo.insert(make_pair(i, info));

            /* 添加歌曲名字至列表 */
            musicList->addItem(info.fileName);
        }
    }
}

void MainWidget::on_musicList_itemClicked(QListWidgetItem *item)
{
    playerQt.play(musicInfo[musicList->row(item)].filePath);
}

void MainWidget::on_nextBtn_clicked()
{
    int index = musicList->currentRow() + 1;
    if((uint32_t)index >= musicInfo.size())
        index = 0;
    musicList->setCurrentRow(index);
    playerQt.play(musicInfo[index].filePath);
}

void MainWidget::on_perBtn_clicked()
{
    int index = musicList->currentRow() - 1;
    if(index < 0)
        index = musicInfo.size() - 1;
    musicList->setCurrentRow(index);
    playerQt.play(musicInfo[index].filePath);
}

void MainWidget::onPlayerEvent(FFplayerQt::PlayerEvent type)
{
    switch (type) {
    case FFplayerQt::FF_PLAY:
        musicBtn[1]->setStyleSheet(playStyle());
        break;
    default:
        break;
    }
}

